package com.laborder.sysyy.controller.LabOrder;


import com.laborder.sysyy.common.Constant;
import com.laborder.sysyy.entity.*;
import com.laborder.sysyy.entity.Dictionary;

import com.laborder.sysyy.service.classes.ClassService;
import com.laborder.sysyy.service.college.CollegeService;
import com.laborder.sysyy.service.course.CourseService;
import com.laborder.sysyy.service.coursetable.CourseTableService;
import com.laborder.sysyy.service.dictionary.DictionaryService;
import com.laborder.sysyy.service.labOrder.LabOrderService;
import com.laborder.sysyy.service.labs.LabsService;
import com.laborder.sysyy.service.log.LogService;
import com.laborder.sysyy.utils.DateUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;


@Controller
@RequestMapping(value = "LabOrder")
public class LabOrderController {
	@Autowired
	private LabOrderService labOrderService;
	@Autowired
	private CourseService courseService;
	@Autowired
	private DictionaryService dictionaryService;
	@Autowired
	private LabsService labsService;
	@Autowired
	private ClassService classService;
	@Autowired
	private CollegeService collegeService;
	@Autowired
	private LogService logService;
	/**
	 * @Function: 返回实验室预约页面数据接口
	 * @author: 786107529@qq.com FJL
	 * @date:2019/1/5 17:45
	 */
	@GetMapping("toLabOrder")
	public String toLabOrder(Model model){
		List<College> collegeList = collegeService.findAllCollege();
		List<Labs> labsList = labsService.getAllLabs();
		model.addAttribute("collegeList",collegeList);
		model.addAttribute("labsList",labsList);
		return "/laborder/labOrderList";
	}
	@ResponseBody
	@RequestMapping("findLabOrderWithPage")
	public Map<String,Object> findLabOrderWithPage(int page, int limit,String cName,String uName,String lName,String loTime){
		Map<String,Object> map = new HashMap<String, Object>();
//		Subject subject = SecurityUtils.getSubject();
//		Session session = subject.getSession();

		map.put("page",(page-1)*limit);
		map.put("limit",limit);
		map.put("cName",cName);
		map.put("uName",uName);
		map.put("lName",lName);
		map.put("loTime",loTime);
		Map<String,Object> resultMap = new HashMap<String, Object>();
		resultMap.put("code",0);
		resultMap.put("msg","");
		resultMap.put("count",labOrderService.getLabOrderCount(map));
		resultMap.put("data",labOrderService.findLabOrderWithPage(map));

		return  resultMap;
	}
	@GetMapping("getAddLabOrder")
	public String getAddLabOrder(Model model){
		List<Course> courseList = courseService.findAllCourse();
		List<College> collegeList = collegeService.findAllCollege();
		List<Classes> classesList = classService.findAllClasses();
		List<Labs> labsList = labsService.getAllLabs();
		List<Dictionary> dictionaryList = dictionaryService.findDictionary();
		List<Dictionary> weekNameList = new ArrayList<Dictionary>();
		List<Dictionary> weekendNameList = new ArrayList<Dictionary>();
		for(Dictionary d:dictionaryList){
			if(d.getDictionaryCode().equals("week")){
				weekNameList.add(d);
			}
		}
		for(Dictionary d:dictionaryList){
			if(d.getDictionaryCode().equals("weekend")){
				weekendNameList.add(d);
			}
		}
		model.addAttribute("weekNameList",weekNameList);
		model.addAttribute("weekendNameList",weekendNameList);
		model.addAttribute("courseList",courseList);
		model.addAttribute("labsList",labsList);
		model.addAttribute("classesList",classesList);
		model.addAttribute("collegeList",collegeList);
		return "/laborder/labOrderAdd";
	}
	@GetMapping("toLabOrderEdit")
	public String toLabOrderEdit(Model model,String loId){
		LabOrder laborderInfo = labOrderService.getLabOrderInfoById(loId);
		Labs labsInfo = labsService.getLabsInfo(laborderInfo.getlId());
		Classes classInfo = classService.getClassInfoById(laborderInfo.getCaId());
		College collegeInfo = collegeService.getCollegeInfo(classInfo.getClId());
		List<Course> courseList = courseService.findAllCourse();
		List<College> collegeList = collegeService.findAllCollege();
		List<Classes> classesList = classService.findAllClasses();
		List<Labs> labsList = labsService.getAllLabs();
		List<Dictionary> dictionaryList = dictionaryService.findDictionary();
		List<Dictionary> weekNameList = new ArrayList<Dictionary>();
		List<Dictionary> weekendNameList = new ArrayList<Dictionary>();
		for(Dictionary d:dictionaryList){
			if(d.getDictionaryCode().equals("week")){
				weekNameList.add(d);
			}
		}
		for(Dictionary d:dictionaryList){
			if(d.getDictionaryCode().equals("weekend")){
				weekendNameList.add(d);
			}
		}
		model.addAttribute("weekNameList",weekNameList);
		model.addAttribute("weekendNameList",weekendNameList);
		model.addAttribute("courseList",courseList);
		model.addAttribute("labsList",labsList);
		model.addAttribute("classesList",classesList);
		model.addAttribute("collegeList",collegeList);
		model.addAttribute("laborderInfo",laborderInfo);
		model.addAttribute("labsInfo",labsInfo);
		model.addAttribute("classInfo",classInfo);
		model.addAttribute("collegeInfo",collegeInfo);
		return "/laborder/labOrderEdit";
	}
	@GetMapping("toLabOrderDetail")
	public String toLabOrderDetail(Model model,String loId){
		LabOrder laborderInfo = labOrderService.getLabOrderInfoById(loId);
		Labs labsInfo = labsService.getLabsInfo(laborderInfo.getlId());
		Classes classInfo = classService.getClassInfoById(laborderInfo.getCaId());
		College collegeInfo = collegeService.getCollegeInfo(classInfo.getClId());
		List<Course> courseList = courseService.findAllCourse();
		List<College> collegeList = collegeService.findAllCollege();
		List<Classes> classesList = classService.findAllClasses();
		List<Labs> labsList = labsService.getAllLabs();
		List<Dictionary> dictionaryList = dictionaryService.findDictionary();
		List<Dictionary> weekNameList = new ArrayList<Dictionary>();
		List<Dictionary> weekendNameList = new ArrayList<Dictionary>();
		for(Dictionary d:dictionaryList){
			if(d.getDictionaryCode().equals("week")){
				weekNameList.add(d);
			}
		}
		for(Dictionary d:dictionaryList){
			if(d.getDictionaryCode().equals("weekend")){
				weekendNameList.add(d);
			}
		}
		model.addAttribute("weekNameList",weekNameList);
		model.addAttribute("weekendNameList",weekendNameList);
		model.addAttribute("courseList",courseList);
		model.addAttribute("labsList",labsList);
		model.addAttribute("classesList",classesList);
		model.addAttribute("collegeList",collegeList);
		model.addAttribute("laborderInfo",laborderInfo);
		model.addAttribute("labsInfo",labsInfo);
		model.addAttribute("classInfo",classInfo);
		model.addAttribute("collegeInfo",collegeInfo);
		return "/laborder/labOrderDetail";
	}
	@ResponseBody
	@RequestMapping("saveLabOrderInfo")
	public  int saveLabOrderInfo(LabOrder laborder,HttpServletRequest request) {
		Subject subject = SecurityUtils.getSubject();
		Session session = subject.getSession();
		User user = (User)session.getAttribute("user");//从当前登录用户的Session中取出user
//		if(user!=null) {
//
//			laborder.setuName(user.getuName());
//		}
		Labs lab = labsService.getLabsInfo(laborder.getlId());
		laborder.setlName(lab.getlName());
		laborder.setLoId(UUID.randomUUID().toString());
		laborder.setApproveState("0");
		laborder.setLoTime(DateUtils.getDateTime());
		laborder.setLockState("0");
		laborder.setAddDate(DateUtils.getDateTime());
		laborder.setUpdateDate(DateUtils.getDateTime());
		logService.save(user.getUserName(),Constant.LABORDER,request);
		return labOrderService.saveLabOrderInfo(laborder);
	}
	@ResponseBody
	@RequestMapping("updateLabOrderInfo")
	public  int updateLabOrderInfo(LabOrder laborder,HttpServletRequest request) {
		Subject subject = SecurityUtils.getSubject();
		Session session = subject.getSession();
		User user = (User)session.getAttribute("user");//从当前登录用户的Session中取出user
//		if(user!=null) {
//
//			laborder.setuName(user.getuName());
//		}
		Labs lab = labsService.getLabsInfo(laborder.getlId());
		laborder.setlName(lab.getlName());
		laborder.setLockState("0");
		laborder.setUpdateDate(DateUtils.getDateTime());
		logService.save(user.getUserName(),Constant.UPDATE_LABORDER,request);
		return labOrderService.updateLabOrderInfo(laborder);
	}
	@ResponseBody
	@RequestMapping("delLabOrderInfo")
	public boolean delLabOrderInfo(String loId,HttpServletRequest request) {

		boolean flag = false;
		int num=0;
		Subject subject = SecurityUtils.getSubject();
		Session session = subject.getSession();
		User user = (User)session.getAttribute("user");//从当前登录用户的Session中取出user
		num = labOrderService.delLabOrderInfo(loId);
		logService.save(user.getUserName(),Constant.DEL_LABORDER,request);
		if(num==0){
			 flag = false;
		}else{
			flag=true;

		}
		return flag;
	}
	@ResponseBody
	@RequestMapping("changeLockState")
	public BussinessMsg changeLockState(String loId, String lockState) {
		BussinessMsg msg = null;
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("loId",loId);
		map.put("lockState","1");
		msg = labOrderService.changeLockState(map);
		return msg;
	}
	@ResponseBody
	@RequestMapping("removeLockState")
	public BussinessMsg removeLockState(String loId, String lockState) {
		BussinessMsg msg = null;
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("loId",loId);
		map.put("lockState","0");
		msg = labOrderService.removeLockState(map);
		return msg;
	}
	@ResponseBody
	@RequestMapping("labOrderDown")
	public BussinessMsg labOrderDown(String loId, String lockState) {
		BussinessMsg msg = null;
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("loId",loId);
		map.put("lockState","-1");
		msg = labOrderService.labOrderDown(map);
		return msg;
	}
	@ResponseBody
	@RequestMapping("delManyLabOrderInfo")
	public boolean delManyLabOrderInfo(@RequestParam("loIds[]") String loIds[]) {
		boolean flag = false;
		int i=0;
		for(String id:loIds){
			i = labOrderService.delLabOrderInfo(id);
		}
		if(i==0){
			flag = false;
		}else{
			flag = true;
		}
		return flag;
	}

	

	
}